#!/usr/bin/env python

import pygtk
pygtk.require("2.0")
import gtk, gobject
from location import Location
from validator import Validator
import threading

class Location_Window:
    def __init__(self,locations,location=None):
        """constructor"""
        
        self.locations = locations
        
        self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
        self.window.connect("destroy", self.destroy)
        self.window.connect("delete_event", self.delete_event)
        
        if location:
            self.window.set_title("Location Settings")
            self.location = Location(location.name)
            self.location.automount = location.automount
            self.location.domain = location.domain
            self.location.shares = location.shares
        else:
            self.window.set_title("Add Location")
            self.location = Location("")
        
        self.Lbl_Name = gtk.Label("Location :")
        self.Lbl_Domain = gtk.Label("Domain:")
        self.TB_Name = gtk.Entry(max=0)
        self.TB_Name.set_text(self.location.name)
        self.TB_Domain = gtk.Entry(max=0)
        self.TB_Domain.set_text(self.location.domain)
        
        #submit button
        self.Btn_Sumbit = gtk.Button("Submit")
        self.Btn_Sumbit.connect("clicked", self.submit)
        
        #Boxes
        self.Name_HBox = gtk.HBox(False, 5)
        self.Domain_HBox = gtk.HBox(False, 5)
        self.Submit_HBox = gtk.HBox(False, 5)
        self.Global_VBox = gtk.VBox(False, 0)
        
        #Packing
        self.Name_HBox.pack_start(self.Lbl_Name, False, False, 10)
        self.Name_HBox.pack_start(self.TB_Name, False, False, 10)
        self.Domain_HBox.pack_start(self.Lbl_Domain, False, False, 10)
        self.Domain_HBox.pack_start(self.TB_Domain, False, False, 10)
        self.Submit_HBox.pack_start(self.Btn_Sumbit, False, False, 10)
        self.Global_VBox.pack_start(self.Name_HBox, False, False, 10)
        self.Global_VBox.pack_start(self.Domain_HBox,False,False,10)
        self.Global_VBox.pack_start(self.Submit_HBox, False, False, 10)
        
        self.window.add(self.Global_VBox)
        #Show
        self.window.show_all()
    
    def Is_Valid(self):
        validator = Validator()
        validator.add(self.TB_Name,"REGEX_NAME",self.Lbl_Name)
        validator.add(self.TB_Domain,"REGEX_DOMAIN",self.Lbl_Domain)
        if validator.isvalid():
            if ( not self.TB_Name.get_text() in self.locations ) or self.location.name == self.TB_Name.get_text():
                return True
        return False

    def submit(self,widget):
        if self.Is_Valid():
            self.location.name = self.TB_Name.get_text()
            self.location.domain = self.TB_Domain.get_text()
            for share_name in self.location.shares:
                self.location.shares[share_name].location = self.location
            self.window.destroy()
        
    def delete_event(self, widget, event):
        """delete_event is call when the close button is pressed"""  
        self.location = None
        return False #False call the destroy signal True don't
    
    def destroy(self, widget):
       gtk.main_quit()
    
    def main(self):
        gtk.main()
        return self.location
if __name__ == '__main__':
    window = Location_Window({"plop" : Location("plop")})
    window.main()